<template>
  <a-card :bordered="false">
    <f-search-form @searchHandler="searchHandler">
      <f-search-form-item label="专业包代码" name="majorCode" slot="advanced" />
      <f-search-form-item label="专业名称" name="majorName"/>
      <f-search-form-item label="工程包代码" name="packageNo" slot="advanced" />
      <f-search-form-item label="工程包名称" name="packageName"/>
      <f-search-form-item label="物料wbs" name="materialWbsCode" slot="advanced" />
      <f-search-form-item label="物料代码" name="materialCode" slot="advanced" />
      <f-search-form-item label="服务wbs" name="serviceWbsCode" slot="advanced" />
      <f-search-form-item label="服务代码" name="serviceCode" slot="advanced" />
      <f-search-form-item label="设备材料名称" name="materialName" slot="advanced" />
      <f-search-form-item label="层级号" name="levelNo" slot="advanced" />
      <f-search-form-item label="科目名称" name="subjectName" slot="advanced" />
      <f-search-form-item label="区域" name="materialArea" slot="advanced" />
      <f-search-form-item label="系统" name="materialUse" slot="advanced"/>
      <f-search-form-item label="历史版本" name="versionId" type="select" ref="versionData" :showAll="true"
                          :autoLoad="true" :data="api.material.selectVersionList" labelKey="versionNo" valueKey="versionId"
                          slot="advanced" :params="queryParam"/>
    </f-search-form>
    <f-table ref="table"  rowKey="materialPlanId" :scroll="{x:3410,y:500}" :operator="operator"  :columns="columns" :multiple="false"  :params="queryParam" :api="api.material" >
      <span slot="action" slot-scope="text, record">
          <template>

            <a @click="editmaterial(record)">编辑</a>
            <a-divider type="vertical"/>
          </template>
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
            <a-menu slot="overlay">
              <a-menu-item v-if="$auth('table.delete')">
             <a href="javascript:;" @click="delmaterial(record)">删除</a>
<!--                      <a href="javascript:;" @click="$refs.table.del(record)">删除</a>-->
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      <button class="ant-btn ant-btn-dashed" type="button" @click="zcSubmit" > 转为100版本</button>
      <button class="ant-btn ant-btn-dashed" type="button" @click="download" > 下载空模板</button>
      <a-upload
        name="file"
        :multiple="false"
        :action="api.material.importExcel"
        :headers="headers"
        :data="projectIds"
        @change="handleChange"
        v-show="false"
      >
        <a-button ref="ubutton"> <a-icon type="upload" /> 导入 </a-button>
      </a-upload>
      <a-upload
        name="file"
        :multiple="false"
        :action="api.material.importExcelTest"
        :headers="headers"
        :data="projectIds"
        @change="handleChangeTest"
        v-show="false"
      >
        <a-button ref="ubuttonTest"> <a-icon type="upload" /> 测试导入 </a-button>
      </a-upload>
      <button class="ant-btn ant-btn-dashed" type="button" @click="importdata" > 导入数据</button>
      <button class="ant-btn ant-btn-dashed" type="button" @click="addData(projectId)"> 添加</button>
      <button class="ant-btn ant-btn-dashed" type="button" @click="downLoadata(projectId)"> 导出清单</button>
      <button class="ant-btn ant-btn-dashed" type="button" @click="alldel(projectId)"> 一键全删</button>
      <button class="ant-btn ant-btn-dashed" type="button" @click="importdataTest">导入清单校验</button>
    </f-table>
    <materbmp-drawer ref="materbmpDrawer"></materbmp-drawer>
    <previewForCk ref="preview" v-bind:projectId="projectId" v-on:changeVisible="changeVisible"></previewForCk>
  </a-card>
</template>

<script>
  import api from './js/api'
  import Vue from 'vue'
  import MaterialsDrawer from './materials_drawer'
  import MaterbmpDrawer from './materials_add_drawer'
  import previewForCk from './preview_for_ck'
  export default {
    name: 'TableList',
    components: {
      MaterialsDrawer,
      MaterbmpDrawer,
      previewForCk
    },
    props: {
      projectId: {
        type: String,
        default: ''
      }
    },
    data() {
      const token = Vue.ls.get('Access-Token')
      let me = this
      return {
        projectIds: {},
        projectStatus: '',
        projectVersion100: '',
        api: Object.assign(api, this.api),
        headers: { 'Authorization': token },
        tableId: 'appUser',
        //列表初始化查询参数
        queryParam: { },
        // 树形缩进
        indentSize: 35,
        // 表头
        columns: [
          {
            title: '专业名称',
            align: 'center',
            dataIndex: 'majorName',
            width: 150
          },
          {
            title: '专业代码',
            align: 'center',
            dataIndex: 'majorCode',
            width: 100

          },
          {
            title: '工程包名称',
            align: 'center',
            dataIndex: 'packageName',
            width: 100
          },
          {
            title: '工程包代码',
            align: 'center',
            dataIndex: 'packageNo',
            width: 100
          },
          {
            title: '材料WBS代码',
            align: 'center',
            dataIndex: 'materialWbsCode',
            width: 100
          },
          {
            title: '全物料代码',
            align: 'center',
            dataIndex: 'materialCode',
            width: 150
          },
          {
            title: '设备、材料名称',
            align: 'center',
            dataIndex: 'materialName',
            width: 150
          },
          // {
          //   title: '型号/规格',
          //   align: 'center',
          //   dataIndex: 'materiaSpecs',
          //   width: 150
          // },
          {
            title: '科目名称',
            align: 'center',
            dataIndex: 'subjectName',
            width: 100
          },
          {
            title: '技术参数或技术说明',
            align: 'center',
            dataIndex: 'technicalData',
            width: 150
          },
          {
            title: '服务WBS代码',
            align: 'center',
            dataIndex: 'serviceWbsCode',
            width: 100
          },
          {
            title: '服务代码',
            align: 'center',
            dataIndex: 'serviceCode',
            width: 100
          },
          {
            title: '项目名称',
            align: 'center',
            dataIndex: 'materiaSpecs',
            width: 150
          },
          {
            title: '层级号',
            align: 'center',
            dataIndex: 'levelNo',
            width: 100
          },
          // {
          //   title: '项目',
          //   align: 'center',
          //   dataIndex: '',
          //   width: 150
          // },
          {
            title: '材料项目描述',
            align: 'center',
            dataIndex: 'materialDesc',
            width: 150
          },
          {
            title: '分承包发包项目描述',
            align: 'center',
            dataIndex: 'packageDesc',
            width: 150
          },
          {
            title: '对外合同清单描述',
            align: 'center',
            dataIndex: 'contractDesc',
            width: 150
          },
          {
            title: '分区域',
            align: 'center',
            dataIndex: 'materialArea',
            width: 150
          },
          {
            title: '分系统',
            align: 'center',
            dataIndex: 'materialUse',
            width: 150
          },
          {
            title: '合同清单工程量',
            align: 'center',
            dataIndex: 'contractBillQuantity',
            width: 150
          },
          {
            title: '标前工程量',
            align: 'center',
            dataIndex: 'beforeMarkQuantity',
            width: 150
          },
          {
            title: '100预算成本工程量',
            align: 'center',
            dataIndex: 'costQuantity100',
            width: 150
          },
          {
            title: '200预算成本工程量',
            align: 'center',
            dataIndex: 'costQuantity200',
            width: 150
          },
          {
            title: '损耗',
            align: 'center',
            dataIndex: 'loss',
            width: 150
          },
          {
            title: '计量单位',
            align: 'center',
            dataIndex: 'measurementUnit',
            width: 150
          },
          {
            title: ' 100采购计划量',
            align: 'center',
            dataIndex: 'planPurchaseCount',
            width: 150
          },
          {
            title: '200采购计划量',
            align: 'center',
            dataIndex: 'planPurchaseCount1',
            width: 150
          },
          {
            title: '完成工程量',
            align: 'center',
            dataIndex: '',
            width: 150,
            children: [
              {
                title: '本月完成工程量',
                align: 'center',
                dataIndex: 'currentFinishMonthCount',
                width: 150
              },
              {
                title: '截止上月完成工程量',
                align: 'center',
                dataIndex: 'lastFinishMonthCount',
                width: 150
              },
              {
                title: '累计完成工程量',
                align: 'center',
                dataIndex: 'totalFinishCount',
                width: 150
              }
            ]
          },
          {
            title: '预算成本单价（未税）',
            align: 'center',
            dataIndex: '',
            width: 150,
            children: [
              {
                title: '单价',
                align: 'center',
                dataIndex: '',
                width: 150,
                children: [
                  {
                    title: '主材价',
                    align: 'center',
                    dataIndex: 'mainPrice',
                    width: 150
                  },
                  {
                    title: '安装价',
                    align: 'center',
                    dataIndex: 'installPrice',
                    width: 150
                  },
                  {
                    title: '综合单价',
                    align: 'center',
                    dataIndex: 'syntheticalPrice',
                    width: 150
                  }
                ]
              },
              {
                title: '合价',
                align: 'center',
                dataIndex: '',
                width: 150,
                children: [
                  {
                    title: '主材费',
                    align: 'center',
                    dataIndex: 'mainAmount',
                    width: 150
                  },
                  {
                    title: '安装费',
                    align: 'center',
                    dataIndex: 'installAmount',
                    width: 150
                  },
                  {
                    title: '合计',
                    align: 'center',
                    dataIndex: 'syntheticalAmount',
                    width: 150
                  }
                ]
              }
            ]
          },
          {
            title: '对外合同单价',
            align: 'center',
            dataIndex: '',
            width: 150,
            children: [
              {
                title: '有合同价（单价）',
                align: 'center',
                dataIndex: '',
                width: 150,
                children: [
                  {
                    title: '主材价',
                    align: 'center',
                    dataIndex: 'mainPrice1',
                    width: 150
                  },
                  {
                    title: '安装价',
                    align: 'center',
                    dataIndex: 'installPrice1',
                    width: 150
                  },
                  {
                    title: '综合单价',
                    align: 'center',
                    dataIndex: 'syntheticalPrice1',
                    width: 150
                  }
                ]
              },
              {
                title: '拟合同价（单价）',
                align: 'center',
                dataIndex: '',
                width: 150,
                children: [
                  {
                    title: '主材价',
                    align: 'center',
                    dataIndex: 'mainPrice2',
                    width: 150
                  },
                  {
                    title: '安装价',
                    align: 'center',
                    dataIndex: 'installPrice2',
                    width: 150
                  },
                  {
                    title: '综合单价',
                    align: 'center',
                    dataIndex: 'syntheticalPrice2',
                    width: 150
                  }
                ]
              },
              {
                title: '合同价（单价）',
                align: 'center',
                dataIndex: '',
                width: 150,
                children: [
                  {
                    title: '主材价',
                    align: 'center',
                    dataIndex: 'mainPrice3',
                    width: 150
                  },
                  {
                    title: '安装价',
                    align: 'center',
                    dataIndex: 'installPrice3',
                    width: 150
                  },
                  {
                    title: '综合单价',
                    align: 'center',
                    dataIndex: 'syntheticalPrice3',
                    width: 150
                  }
                ]
              }
            ]
          },
          {
            title: '预估总产值',
            align: 'center',
            dataIndex: '',
            width: 150,
            children: [
              {
                title: '有合同价（合价）',
                align: 'center',
                dataIndex: '',
                width: 150,
                children: [
                  {
                    title: '主材价',
                    align: 'center',
                    dataIndex: 'mainsumPrice1',
                    width: 150
                  },
                  {
                    title: '安装价',
                    align: 'center',
                    dataIndex: 'installsumPrice1',
                    width: 150
                  },
                  {
                    title: '综合单价',
                    align: 'center',
                    dataIndex: 'contractsumSyntheticalPrice1',
                    width: 150
                  }
                ]
              },
              {
                title: '拟合同价（合价）',
                align: 'center',
                dataIndex: '',
                width: 150,
                children: [
                  {
                    title: '主材价',
                    align: 'center',
                    dataIndex: 'mainsumPrice2',
                    width: 150
                  },
                  {
                    title: '安装价',
                    align: 'center',
                    dataIndex: 'installsumPrice2',
                    width: 150
                  },
                  {
                    title: '综合单价',
                    align: 'center',
                    dataIndex: 'contractsumSyntheticalPrice2',
                    width: 150
                  }
                ]
              },
              {
                title: '合同价（合价）',
                align: 'center',
                dataIndex: '',
                width: 150,
                children: [
                  {
                    title: '主材价',
                    align: 'center',
                    dataIndex: 'mainsumPrice3',
                    width: 150
                  },
                  {
                    title: '安装价',
                    align: 'center',
                    dataIndex: 'installsumPrice3',
                    width: 150
                  },
                  {
                    title: '综合单价',
                    align: 'center',
                    dataIndex: 'contractsumSyntheticalPrice3',
                    width: 150
                  }
                ]
              }
            ]
          },
          {
            title: '本月实现产值（未税）',
            align: 'center',
            dataIndex: '',
            width: 150,
            children: [
              {
                title: '合同价（合价）',
                align: 'center',
                dataIndex: '',
                width: 150,
                children: [
                  {
                    title: '主材价',
                    align: 'center',
                    dataIndex: 'currentMonthmainPrice',
                    width: 150
                  },
                  {
                    title: '安装价',
                    align: 'center',
                    dataIndex: 'currentMonthinstallPrice',
                    width: 150
                  },
                  {
                    title: '合计',
                    align: 'center',
                    dataIndex: 'currentMonthsumPrice',
                    width: 150
                  }
                ]
              }
            ]
          },
          {
            title: '截止上月实现产值（未税）',
            align: 'center',
            dataIndex: '',
            width: 150,
            children: [
              {
                title: '合同价（合价）',
                align: 'center',
                dataIndex: '',
                width: 150,
                children: [
                  {
                    title: '主材价',
                    align: 'center',
                    dataIndex: 'lastMonthmainPrice',
                    width: 150
                  },
                  {
                    title: '安装价',
                    align: 'center',
                    dataIndex: 'lastMonthinstallPrice',
                    width: 150
                  },
                  {
                    title: '合计',
                    align: 'center',
                    dataIndex: 'lastMonthsumPrice',
                    width: 150
                  }
                ]
              }
            ]
          },
          {
            title: '累计实现产值（未税）',
            align: 'center',
            dataIndex: '',
            width: 150,
            children: [
              {
                title: '合同价（合价）',
                align: 'center',
                dataIndex: '',
                width: 150,
                children: [
                  {
                    title: '主材价',
                    align: 'center',
                    dataIndex: 'totalMonthmainPrice',
                    width: 150
                  },
                  {
                    title: '安装价',
                    align: 'center',
                    dataIndex: 'totalMonthinstallPrice',
                    width: 150
                  },
                  {
                    title: '合计',
                    align: 'center',
                    dataIndex: 'totalsumPrice',
                    width: 150
                  }
                ]
              }
            ]
          },
          {
            title: '品牌',
            align: 'center',
            dataIndex: 'brandName',
            width: 150
          },
          {
            title: '备注',
            align: 'center',
            dataIndex: 'remark',
            width: 150
          },
          {
            title: '操作',
            align: 'center',
            scopedSlots: { customRender: 'action' },
            width: 150,
            fixed: 'right'
          }

        ],
        operator: [
          // {
          //   name: '删除',
          //   icon: 'delete',
          //   batch: true,
          //   handler: function (table) {
          //     debugger
          //     table.remove()
          //   }
          // }
        ]
      }
    },
    created() {
      this.projectIds.projectId = this.projectId
      this.queryParam.projectId = this.projectId
      this.http.get(this, {
        url: this.api.material.projectStatus,
        data: {
          projectId: this.projectId
        },
        success: function (me, data) {
          me.projectStatus = data
        }
      })
      this.http.get(this, {
        url: this.api.material.getprojectVersion100,
        data: {
          projectId: this.projectId
        },
        success: function (me, data) {
          me.projectVersion100 = data
        }
      })
    },
    methods: {
      //子界面隐藏时刷新界面
      changeVisible () {
        this.$refs.table.localLoading = false
        this.$refs.table.refresh()
      },
      editmaterial(record) {
        const vesrionStatus = record.status
        if (vesrionStatus < 1) {
          this.$refs.materbmpDrawer.showDrawer(record)
        } else {
          this.$message.error('当前材料版本不支持编辑')
        }
      },
      searchHandler(params) {
        this.$refs.table.refresh(true, params)
      },
      download() {
          window.location.href = '/母表导入模板.xlsx'
      },
      handleChange: function(info) {
        this.$refs.table.localLoading = true
        if (info.file.status === 'done') {
          if (info.file.response.code === 100) {
            // this.checkImport()
            this.$message.success('导入成功')
            this.$refs.table.localLoading = false
            this.$refs.table.refresh()
          } else {
            this.$refs.table.localLoading = false
            this.$message.error(info.file.response.message)
          }
        } else if (info.file.status === 'error') {
          this.$refs.table.localLoading = false
          this.$message.error('导入失败')
        }
      },
      handleChangeTest: function(info) {
        if (info.file.status === 'done') {
          if (info.file.response.code === 100) {
            this.$message.success('模拟校验开始')
            this.$refs['preview'].handleShow(projectId)

          } else {
            this.$message.error(info.file.response.message)
          }
        } else if (info.file.status === 'error') {
          this.$message.error('导入失败')
        }
      },
      addData(info) {
        if (this.projectVersion100 === 1) {
            this.$message.error('项目已转存100不支持添加数据')
            return
        }
        if (this.projectStatus === 1) {
          this.$message.error('项目已锁定不支持操作')
          return
        }
        this.$refs.materbmpDrawer.showDrawer(info)
      },
      downLoadata(){
        // if (this.projectStatus === 1) {
        //   this.$message.error('项目已锁定不支持操作')
        //   return
        // }
        let columns = this.$refs.table.getExportColumns(this.columns)
        this.queryParam['columns'] = JSON.stringify(columns)
        this.http.exportExcel(this, {
          url: this.api.material.exportUrl,
          data: this.queryParam
        })
      },
      importdata() {
        if (this.projectVersion100 === 1) {
          this.$message.error('项目已转存100不支持导入数据')
          return
        }
        if ( this.projectStatus === 1){
          this.$message.error('项目已锁定不支持操作')
             return
        }
        this.$refs.ubutton.$el.click()
      },
      importdataTest() {
        this.$refs['preview'].handleShow(this.projectId)
        // if (this.projectVersion100 === 1) {
        //   this.$message.error('项目已转存100不支持导入数据')
        //   return
        // }
        // if ( this.projectStatus === 1){
        //   this.$message.error('项目已锁定不支持操作')
        //      return
        // }
        // this.$refs.ubuttonTest.$el.click()
      },
      getSelectedRowKey() {
        return this.$refs.table.getSelectedRowKey()
      },
      zcSubmit() {
        if (this.projectVersion100 === 1) {
          this.$message.error('项目已转存100')
          return
        }
        if ( this.projectStatus === 1) {
          this.$message.error('项目已锁定不支持操作')
          return
        }
        let selectIds = this.$refs.table.getSelectedRowKey()
        // if (!selectIds || selectIds.length === 0) {
        //   this.$message.error('您还未选中数据')
        //   return
        // }
        let pId = this.projectId
        this.http.post(this, {
          url: this.api.material.zcversion,
          data: {
            materialPlanIds: selectIds.join(','),
            projectId: pId
          },
          success: function(me, data) {
            me.$message.success(data)
            me.$refs.table.refresh(true)
          }
        })
      },
      delmaterial(data) {
        if (this.projectVersion100 === 1) {
          this.$message.error('项目已转存100，不支持该操作')
          return
        }
        if (this.projectStatus === 1) {
          this.$message.error('项目已锁定不支持操作')
          return
        }
        this.$confirm({
          title: '确认删除',
          content: '您确认要删除所选数据吗?',
          onOk: () => {
            let pId = this.projectId
            this.http.post(this, {
              url: this.api.material.remove,
              data: {
                ids: data.materialPlanId,
                projectId: pId
              },
              success: function (me, data) {
                me.$message.success(data)
                me.$refs.table.refresh(true)
              }
            })
          }
         })
       },
      alldel(data) {
        if (this.projectVersion100 === 1) {
          this.$message.error('项目已转存100，不支持该操作')
          return
        }
        if (this.projectStatus === 1) {
          this.$message.error('项目已锁定不支持操作')
          return
        }
        this.$confirm({
          title: '确认删除',
          content: '您确认要删除所有数据吗?',
          onOk: () => {
            let pId = this.projectId
            this.http.post(this, {
              url: this.api.material.allremove,
              data: {
                projectId: pId
              },
              success: function(me, data) {
                me.$message.success(data)
                me.$refs.table.refresh(true)
              }
            })
          }
        })
       }
      }
  }
</script>
<style scoped>

 </style>